#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int missingNumber(vector<int>& nums)
    {
        int left = 0;
        int right = nums.size() - 1;

        while (left <= right)
        {
            int mid = (left + right) / 2;

            if (nums[mid] == mid)
            {
                left = mid + 1;
            }
            else  //nums[mid] != mid
            {
                if (mid == 0 || nums[mid - 1] == mid - 1)
                {
                    return mid;
                }
                else
                {
                    right = mid - 1;
                }
            }
        }

        if (left == nums.size())
        {
            return left;
        }

        return -1;
    }
};